package com.demo.generate.code.service;

import com.demo.generate.code.config.CodeGeneratorConfig;
import com.demo.generate.code.service.TableInfoService.ColumnInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 代码生成服务类
 */
@Service
public class CodeGeneratorService implements CodeGeneratorInterface {
    
    @Autowired
    private CodeGeneratorConfig generatorConfig;
    
    @Autowired
    private TableInfoService tableInfoService;
    
    /**
     * 生成代码
     * 
     * @param tableName 表名
     * @throws SQLException SQL异常
     * @throws IOException IO异常
     */
    @Override
    public void generateCode(String tableName) throws SQLException, IOException {
        // 获取列信息
        List<ColumnInfo> columns = tableInfoService.getColumns(tableName);
        
        // 生成实体类名
        String entityName = toCamelCase(tableName, true);
        
        // 生成实体类
        generateEntity(entityName, tableName, columns);
        
        // 生成代码
        if (generatorConfig.getGenerator().isGenerateController()) {
            generateController(entityName, tableName);
        }
        
        if (generatorConfig.getGenerator().isGenerateService()) {
            generateService(entityName, tableName);
        }
        
        if (generatorConfig.getGenerator().isGenerateDao()) {
            generateDao(entityName, tableName, columns);
        }
        
        if (generatorConfig.getGenerator().isGenerateMapperXml()) {
            generateMapperXml(entityName, tableName, columns);
        }
    }
    
    /**
     * 生成实体类代码
     * 
     * @param entityName 实体类名
     * @param tableName 表名
     * @param columns 列信息
     * @throws IOException IO异常
     */
    public void generateEntity(String entityName, String tableName, List<ColumnInfo> columns) throws IOException {
        String basePackage = generatorConfig.getGenerator().getBasePackage();
        String author = generatorConfig.getGenerator().getAuthor();
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        
        StringBuilder code = new StringBuilder();
        code.append("package ").append(basePackage).append(".model;\n\n");
        
        code.append("import lombok.Data;\n");
        code.append("import java.time.LocalDateTime;\n");
        code.append("import java.time.LocalDate;\n\n");
        
        code.append("/**\n");
        code.append(" * ").append(entityName).append("实体类\n");
        code.append(" *\n");
        code.append(" * @author ").append(author).append("\n");
        code.append(" * @since ").append(dateTime).append("\n");
        code.append(" */\n");
        code.append("@Data\n");
        code.append("public class ").append(entityName).append(" {\n\n");
        
        // 生成字段
        for (ColumnInfo column : columns) {
            if (column.getRemarks() != null && !column.getRemarks().isEmpty()) {
                code.append("    /**\n");
                code.append("     * ").append(column.getRemarks()).append("\n");
                code.append("     */\n");
            }
            
            code.append("    private ").append(getJavaType(column.getDataType())).append(" ")
                .append(toCamelCase(column.getColumnName(), false)).append(";\n\n");
        }
        
        code.append("}\n");
        
        // 写入文件
        String outputPath = generatorConfig.getGenerator().getOutputDir() + "/model/" + entityName + ".java";
        writeToFile(outputPath, code.toString());
    }
    
    /**
     * 生成Controller代码
     * 
     * @param entityName 实体类名
     * @param tableName 表名
     * @throws IOException IO异常
     */
    private void generateController(String entityName, String tableName) throws IOException {
        String basePackage = generatorConfig.getGenerator().getBasePackage();
        String author = generatorConfig.getGenerator().getAuthor();
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        
        StringBuilder code = new StringBuilder();
        code.append("package ").append(basePackage).append(".controller;\n\n");
        code.append("import ").append(basePackage).append(".service.")
            .append(entityName).append("Service;\n");
        code.append("import ").append(basePackage).append(".model.")
            .append(entityName).append(";\n");
        code.append("import org.springframework.beans.factory.annotation.Autowired;\n");
        code.append("import org.springframework.web.bind.annotation.*;\n\n");
        code.append("import java.util.List;\n\n");
        
        code.append("/**\n");
        code.append(" * ").append(entityName).append(" Controller\n");
        code.append(" *\n");
        code.append(" * @author ").append(author).append("\n");
        code.append(" * @since ").append(dateTime).append("\n");
        code.append(" */\n");
        code.append("@RestController\n");
        code.append("@RequestMapping(\"/").append(toCamelCase(tableName, false)).append("\")\n");
        code.append("public class ").append(entityName).append("Controller {\n\n");
        
        code.append("    @Autowired\n");
        code.append("    private ").append(entityName).append("Service ").append(toCamelCase(entityName, false))
            .append("Service;\n\n");
        
        code.append("    /**\n");
        code.append("     * 查询所有").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @return ").append(entityName).append("列表\n");
        code.append("     */\n");
        code.append("    @GetMapping\n");
        code.append("    public List<").append(entityName).append("> selectAll() {\n");
        code.append("        return ").append(toCamelCase(entityName, false)).append("Service.selectAll();\n");
        code.append("    }\n\n");
        
        code.append("    /**\n");
        code.append("     * 根据ID查询").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param id ID\n");
        code.append("     * @return ").append(entityName).append("\n");
        code.append("     */\n");
        code.append("    @GetMapping(\"/{id}\")\n");
        code.append("    public ").append(entityName).append(" selectById(@PathVariable Long id) {\n");
        code.append("        return ").append(toCamelCase(entityName, false)).append("Service.selectById(id);\n");
        code.append("    }\n\n");
        
        code.append("    /**\n");
        code.append("     * 新增").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param ").append(toCamelCase(entityName, false)).append(" ").append(entityName).append("\n");
        code.append("     * @return 是否成功\n");
        code.append("     */\n");
        code.append("    @PostMapping\n");
        code.append("    public boolean insert(@RequestBody ").append(entityName).append(" ")
            .append(toCamelCase(entityName, false)).append(") {\n");
        code.append("        return ").append(toCamelCase(entityName, false)).append("Service.insert(")
            .append(toCamelCase(entityName, false)).append(");\n");
        code.append("    }\n\n");
        
        code.append("    /**\n");
        code.append("     * 更新").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param ").append(toCamelCase(entityName, false)).append(" ").append(entityName).append("\n");
        code.append("     * @return 是否成功\n");
        code.append("     */\n");
        code.append("    @PutMapping\n");
        code.append("    public boolean update(@RequestBody ").append(entityName).append(" ")
            .append(toCamelCase(entityName, false)).append(") {\n");
        code.append("        return ").append(toCamelCase(entityName, false)).append("Service.update(")
            .append(toCamelCase(entityName, false)).append(");\n");
        code.append("    }\n\n");
        
        code.append("    /**\n");
        code.append("     * 根据ID删除").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param id ID\n");
        code.append("     * @return 是否成功\n");
        code.append("     */\n");
        code.append("    @DeleteMapping(\"/{id}\")\n");
        code.append("    public boolean delete(@PathVariable Long id) {\n");
        code.append("        return ").append(toCamelCase(entityName, false)).append("Service.delete(id);\n");
        code.append("    }\n");
        
        code.append("}\n");
        
        // 写入文件
        String outputPath = generatorConfig.getGenerator().getOutputDir() + "/controller/" + entityName + "Controller.java";
        writeToFile(outputPath, code.toString());
    }
    
    /**
     * 生成Service代码
     * 
     * @param entityName 实体类名
     * @param tableName 表名
     * @throws IOException IO异常
     */
    private void generateService(String entityName, String tableName) throws IOException {
        String basePackage = generatorConfig.getGenerator().getBasePackage();
        String author = generatorConfig.getGenerator().getAuthor();
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        
        // 生成Service接口
        StringBuilder interfaceCode = new StringBuilder();
        interfaceCode.append("package ").append(basePackage).append(".service;\n\n");
        interfaceCode.append("import ").append(basePackage).append(".model.")
            .append(entityName).append(";\n");
        interfaceCode.append("import java.util.List;\n\n");
        
        interfaceCode.append("/**\n");
        interfaceCode.append(" * ").append(entityName).append(" Service接口\n");
        interfaceCode.append(" *\n");
        interfaceCode.append(" * @author ").append(author).append("\n");
        interfaceCode.append(" * @since ").append(dateTime).append("\n");
        interfaceCode.append(" */\n");
        interfaceCode.append("public interface ").append(entityName).append("Service {\n\n");
        
        interfaceCode.append("    /**\n");
        interfaceCode.append("     * 查询所有").append(entityName).append("\n");
        interfaceCode.append("     *\n");
        interfaceCode.append("     * @return ").append(entityName).append("列表\n");
        interfaceCode.append("     */\n");
        interfaceCode.append("    List<").append(entityName).append("> selectAll();\n\n");
        
        interfaceCode.append("    /**\n");
        interfaceCode.append("     * 根据ID查询").append(entityName).append("\n");
        interfaceCode.append("     *\n");
        interfaceCode.append("     * @param id ID\n");
        interfaceCode.append("     * @return ").append(entityName).append("\n");
        interfaceCode.append("     */\n");
        interfaceCode.append("    ").append(entityName).append(" selectById(Long id);\n\n");
        
        interfaceCode.append("    /**\n");
        interfaceCode.append("     * 新增").append(entityName).append("\n");
        interfaceCode.append("     *\n");
        interfaceCode.append("     * @param ").append(toCamelCase(entityName, false)).append(" ").append(entityName).append("\n");
        interfaceCode.append("     * @return 是否成功\n");
        interfaceCode.append("     */\n");
        interfaceCode.append("    boolean insert(").append(entityName).append(" ").append(toCamelCase(entityName, false)).append(");\n\n");
        
        interfaceCode.append("    /**\n");
        interfaceCode.append("     * 更新").append(entityName).append("\n");
        interfaceCode.append("     *\n");
        interfaceCode.append("     * @param ").append(toCamelCase(entityName, false)).append(" ").append(entityName).append("\n");
        interfaceCode.append("     * @return 是否成功\n");
        interfaceCode.append("     */\n");
        interfaceCode.append("    boolean update(").append(entityName).append(" ").append(toCamelCase(entityName, false)).append(");\n\n");
        
        interfaceCode.append("    /**\n");
        interfaceCode.append("     * 根据ID删除").append(entityName).append("\n");
        interfaceCode.append("     *\n");
        interfaceCode.append("     * @param id ID\n");
        interfaceCode.append("     * @return 是否成功\n");
        interfaceCode.append("     */\n");
        interfaceCode.append("    boolean delete(Long id);\n");
        
        interfaceCode.append("}\n");
        
        // 写入文件
        String outputPath = generatorConfig.getGenerator().getOutputDir() + "/service/" + entityName + "Service.java";
        writeToFile(outputPath, interfaceCode.toString());
        
        // 生成Service实现类
        StringBuilder implCode = new StringBuilder();
        implCode.append("package ").append(basePackage).append(".service.impl;\n\n");
        implCode.append("import ").append(basePackage).append(".dao.")
            .append(entityName).append("Dao;\n");
        implCode.append("import ").append(basePackage).append(".model.")
            .append(entityName).append(";\n");
        implCode.append("import ").append(basePackage).append(".service.")
            .append(entityName).append("Service;\n");
        implCode.append("import org.springframework.beans.factory.annotation.Autowired;\n");
        implCode.append("import org.springframework.stereotype.Service;\n");
        implCode.append("import java.util.List;\n\n");
        
        implCode.append("/**\n");
        implCode.append(" * ").append(entityName).append(" Service实现类\n");
        implCode.append(" *\n");
        implCode.append(" * @author ").append(author).append("\n");
        implCode.append(" * @since ").append(dateTime).append("\n");
        implCode.append(" */\n");
        implCode.append("@Service\n");
        implCode.append("public class ").append(entityName).append("ServiceImpl implements ").append(entityName).append("Service {\n\n");
        
        implCode.append("    @Autowired\n");
        implCode.append("    private ").append(entityName).append("Dao ").append(toCamelCase(entityName, false)).append("Dao;\n\n");
        
        implCode.append("    @Override\n");
        implCode.append("    public List<").append(entityName).append("> selectAll() {\n");
        implCode.append("        return ").append(toCamelCase(entityName, false)).append("Dao.selectAll();\n");
        implCode.append("    }\n\n");
        
        implCode.append("    @Override\n");
        implCode.append("    public ").append(entityName).append(" selectById(Long id) {\n");
        implCode.append("        return ").append(toCamelCase(entityName, false)).append("Dao.selectById(id);\n");
        implCode.append("    }\n\n");
        
        implCode.append("    @Override\n");
        implCode.append("    public boolean insert(").append(entityName).append(" ").append(toCamelCase(entityName, false)).append(") {\n");
        implCode.append("        return ").append(toCamelCase(entityName, false)).append("Dao.insert(")
            .append(toCamelCase(entityName, false)).append(") > 0;\n");
        implCode.append("    }\n\n");
        
        implCode.append("    @Override\n");
        implCode.append("    public boolean update(").append(entityName).append(" ").append(toCamelCase(entityName, false)).append(") {\n");
        implCode.append("        return ").append(toCamelCase(entityName, false)).append("Dao.update(")
            .append(toCamelCase(entityName, false)).append(") > 0;\n");
        implCode.append("    }\n\n");
        
        implCode.append("    @Override\n");
        implCode.append("    public boolean delete(Long id) {\n");
        implCode.append("        return ").append(toCamelCase(entityName, false)).append("Dao.delete(id) > 0;\n");
        implCode.append("    }\n");
        
        implCode.append("}\n");
        
        // 写入文件
        String implOutputPath = generatorConfig.getGenerator().getOutputDir() + "/service/impl/" + entityName + "ServiceImpl.java";
        writeToFile(implOutputPath, implCode.toString());
    }
    
    /**
     * 生成DAO代码
     * 
     * @param entityName 实体类名
     * @param tableName 表名
     * @param columns 列信息
     * @throws IOException IO异常
     */
    private void generateDao(String entityName, String tableName, List<ColumnInfo> columns) throws IOException {
        String basePackage = generatorConfig.getGenerator().getBasePackage();
        String author = generatorConfig.getGenerator().getAuthor();
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        
        StringBuilder code = new StringBuilder();
        code.append("package ").append(basePackage).append(".dao;\n\n");
        code.append("import ").append(basePackage).append(".model.")
            .append(entityName).append(";\n");
        code.append("import org.apache.ibatis.annotations.*;\n");
        code.append("import java.util.List;\n\n");
        
        code.append("/**\n");
        code.append(" * ").append(entityName).append(" DAO接口\n");
        code.append(" *\n");
        code.append(" * @author ").append(author).append("\n");
        code.append(" * @since ").append(dateTime).append("\n");
        code.append(" */\n");
        code.append("@Mapper\n");
        code.append("public interface ").append(entityName).append("Dao {\n\n");
        
        code.append("    /**\n");
        code.append("     * 查询所有").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @return ").append(entityName).append("列表\n");
        code.append("     */\n");
        code.append("    @Select(\"SELECT * FROM ").append(tableName).append("\")\n");
        code.append("    List<").append(entityName).append("> selectAll();\n\n");
        
        code.append("    /**\n");
        code.append("     * 根据ID查询").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param id ID\n");
        code.append("     * @return ").append(entityName).append("\n");
        code.append("     */\n");
        code.append("    @Select(\"SELECT * FROM ").append(tableName).append(" WHERE id = #{id}\")\n");
        code.append("    ").append(entityName).append(" selectById(Long id);\n\n");
        
        // 生成插入语句
        StringBuilder insertFields = new StringBuilder();
        StringBuilder insertValues = new StringBuilder();
        for (int i = 0; i < columns.size(); i++) {
            ColumnInfo column = columns.get(i);
            if (!"id".equalsIgnoreCase(column.getColumnName())) { // 跳过ID字段
                if (insertFields.length() > 0) {
                    insertFields.append(", ");
                    insertValues.append(", ");
                }
                insertFields.append(column.getColumnName());
                insertValues.append("#{").append(toCamelCase(column.getColumnName(), false)).append("}");
            }
        }
        
        code.append("    /**\n");
        code.append("     * 新增").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param ").append(toCamelCase(entityName, false)).append(" ").append(entityName).append("\n");
        code.append("     * @return 影响行数\n");
        code.append("     */\n");
        code.append("    @Insert(\"INSERT INTO ").append(tableName).append(" (").append(insertFields)
            .append(") VALUES (").append(insertValues).append(")\")\n");
        code.append("    @Options(useGeneratedKeys = true, keyProperty = \"id\")\n");
        code.append("    int insert(").append(entityName).append(" ").append(toCamelCase(entityName, false)).append(");\n\n");
        
        // 生成更新语句
        StringBuilder updateFields = new StringBuilder();
        for (int i = 0; i < columns.size(); i++) {
            ColumnInfo column = columns.get(i);
            if (!"id".equalsIgnoreCase(column.getColumnName())) { // 跳过ID字段
                if (updateFields.length() > 0) {
                    updateFields.append(", ");
                }
                updateFields.append(column.getColumnName()).append(" = #{")
                    .append(toCamelCase(column.getColumnName(), false)).append("}");
            }
        }
        
        code.append("    /**\n");
        code.append("     * 更新").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param ").append(toCamelCase(entityName, false)).append(" ").append(entityName).append("\n");
        code.append("     * @return 影响行数\n");
        code.append("     */\n");
        code.append("    @Update(\"UPDATE ").append(tableName).append(" SET ").append(updateFields)
            .append(" WHERE id = #{id}\")\n");
        code.append("    int update(").append(entityName).append(" ").append(toCamelCase(entityName, false)).append(");\n\n");
        
        code.append("    /**\n");
        code.append("     * 根据ID删除").append(entityName).append("\n");
        code.append("     *\n");
        code.append("     * @param id ID\n");
        code.append("     * @return 影响行数\n");
        code.append("     */\n");
        code.append("    @Delete(\"DELETE FROM ").append(tableName).append(" WHERE id = #{id}\")\n");
        code.append("    int delete(Long id);\n");
        
        code.append("}\n");
        
        // 写入文件
        String outputPath = generatorConfig.getGenerator().getOutputDir() + "/dao/" + entityName + "Dao.java";
        writeToFile(outputPath, code.toString());
    }
    
    /**
     * 生成Mapper XML代码
     * 
     * @param entityName 实体类名
     * @param tableName 表名
     * @param columns 列信息
     * @throws IOException IO异常
     */
    private void generateMapperXml(String entityName, String tableName, List<ColumnInfo> columns) throws IOException {
        String basePackage = generatorConfig.getGenerator().getBasePackage();
        
        StringBuilder code = new StringBuilder();
        code.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        code.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" ")
            .append("\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n\n");
        code.append("<mapper namespace=\"").append(basePackage).append(".dao.")
            .append(entityName).append("Dao\">\n\n");
        
        // 生成resultMap
        code.append("    <resultMap id=\"").append(entityName).append("ResultMap\" type=\"")
            .append(basePackage).append(".model.").append(entityName).append("\">\n");
        code.append("        <id property=\"id\" column=\"id\"/>\n");
        for (ColumnInfo column : columns) {
            if (!"id".equalsIgnoreCase(column.getColumnName())) {
                code.append("        <result property=\"").append(toCamelCase(column.getColumnName(), false))
                    .append("\" column=\"").append(column.getColumnName()).append("\"/>\n");
            }
        }
        code.append("    </resultMap>\n\n");
        
        // 生成查询语句
        code.append("    <select id=\"selectAll\" resultMap=\"").append(entityName).append("ResultMap\">\n");
        code.append("        SELECT * FROM ").append(tableName).append("\n");
        code.append("    </select>\n\n");
        
        code.append("    <select id=\"selectById\" parameterType=\"Long\" resultMap=\"").append(entityName).append("ResultMap\">\n");
        code.append("        SELECT * FROM ").append(tableName).append(" WHERE id = #{id}\n");
        code.append("    </select>\n\n");
        
        // 生成插入语句
        StringBuilder insertFields = new StringBuilder();
        StringBuilder insertValues = new StringBuilder();
        for (int i = 0; i < columns.size(); i++) {
            ColumnInfo column = columns.get(i);
            if (!"id".equalsIgnoreCase(column.getColumnName())) { // 跳过ID字段
                if (insertFields.length() > 0) {
                    insertFields.append(", ");
                    insertValues.append(", ");
                }
                insertFields.append(column.getColumnName());
                insertValues.append("#{").append(toCamelCase(column.getColumnName(), false)).append("}");
            }
        }
        
        code.append("    <insert id=\"insert\" parameterType=\"").append(basePackage)
            .append(".model.").append(entityName).append("\" useGeneratedKeys=\"true\" keyProperty=\"id\">\n");
        code.append("        INSERT INTO ").append(tableName).append(" (").append(insertFields)
            .append(") VALUES (").append(insertValues).append(")\n");
        code.append("    </insert>\n\n");
        
        // 生成更新语句
        StringBuilder updateFields = new StringBuilder();
        for (int i = 0; i < columns.size(); i++) {
            ColumnInfo column = columns.get(i);
            if (!"id".equalsIgnoreCase(column.getColumnName())) { // 跳过ID字段
                if (updateFields.length() > 0) {
                    updateFields.append(", ");
                }
                updateFields.append(column.getColumnName()).append(" = #{")
                    .append(toCamelCase(column.getColumnName(), false)).append("}");
            }
        }
        
        code.append("    <update id=\"update\" parameterType=\"").append(basePackage)
            .append(".model.").append(entityName).append("\">\n");
        code.append("        UPDATE ").append(tableName).append(" SET ").append(updateFields)
            .append(" WHERE id = #{id}\n");
        code.append("    </update>\n\n");
        
        code.append("    <delete id=\"delete\" parameterType=\"Long\">\n");
        code.append("        DELETE FROM ").append(tableName).append(" WHERE id = #{id}\n");
        code.append("    </delete>\n\n");
        
        code.append("</mapper>\n");
        
        // 写入文件
        String outputPath = generatorConfig.getGenerator().getOutputDir() + "/mapper/" + entityName + "Mapper.xml";
        writeToFile(outputPath, code.toString());
    }
    
    /**
     * 将下划线命名转换为驼峰命名
     * 
     * @param name 下划线命名
     * @param firstUpper 首字母是否大写
     * @return 驼峰命名
     */
    private String toCamelCase(String name, boolean firstUpper) {
        if (!StringUtils.hasText(name)) {
            return name;
        }
        
        StringBuilder result = new StringBuilder();
        boolean nextUpper = firstUpper;
        
        for (char c : name.toCharArray()) {
            if (c == '_') {
                nextUpper = true;
            } else {
                if (nextUpper) {
                    result.append(Character.toUpperCase(c));
                    nextUpper = false;
                } else {
                    result.append(Character.toLowerCase(c));
                }
            }
        }
        
        return result.toString();
    }
    
    /**
     * 根据数据库类型获取Java类型
     * 
     * @param dataType 数据库类型
     * @return Java类型
     */
    private String getJavaType(String dataType) {
        if (dataType == null) {
            return "String";
        }
        
        String upperDataType = dataType.toUpperCase();
        
        if (upperDataType.contains("INT")) {
            if (upperDataType.contains("BIGINT")) {
                return "Long";
            } else {
                return "Integer";
            }
        } else if (upperDataType.contains("VARCHAR") || upperDataType.contains("TEXT") || upperDataType.contains("CHAR")) {
            return "String";
        } else if (upperDataType.contains("DATETIME") || upperDataType.contains("TIMESTAMP")) {
            return "LocalDateTime";
        } else if (upperDataType.contains("DATE")) {
            return "LocalDate";
        } else if (upperDataType.contains("DECIMAL") || upperDataType.contains("DOUBLE")) {
            return "Double";
        } else if (upperDataType.contains("FLOAT")) {
            return "Float";
        } else if (upperDataType.contains("BOOLEAN") || upperDataType.contains("BIT")) {
            return "Boolean";
        } else {
            return "String";
        }
    }
    
    /**
     * 写入文件
     * 
     * @param filePath 文件路径
     * @param content 文件内容
     * @throws IOException IO异常
     */
    private void writeToFile(String filePath, String content) throws IOException {
        File file = new File(filePath);
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
        
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(content);
        }
    }
}